Script Crusher
Published 2007-12-13 09:54:46
Heres the problem.. as soon as you start building large applications usings ExtJs your users say two things
To address these issues, there are a few javascript compression tools out there, however, most have rather painful setup requirements, like getting java working, and are not very hacker friendly.
Now that all my interface is embedded into javascript code, including all the button labels etc. l have been wondering for quite a while how I was going to translate the application and keep it up-to-date (in the Flexy template engine we extract the strings from the HTML, and have a tool that manages PO files based on them). Since this did not seem feasible for Javascript, I was begining to dread the thought of finding all the strings and wrapping them with something like a gettext type call.
So it turned out my little dmdscript hacking may have some use...
It took a small amount of hacking to add tokenizing support to dmdscript, by adding a new class for the javascript interface and few minor changes to dmdscript's internal tokenizer I got this little script to work.
Basically it reads the file, then creates an array of objects, each one representing a token. (I should perhaps change the token to an integer and useĀ Script.Token.whitespace as a constant... ).
The script will basically strip all the whitespace and comments, and convert whitespace into either a line break or a space.. (as linebreaks are actually relivant in Javascript).
The next issue was how to deal with language conversion. The code above can be used to test for strings by doing:
So I came up with the idea of using the quote style to determine if a string needs translating.
single quotes = does not need translating
double quotes = a translable string.
It was amazingly easy to change my existing code to do this, I could then generate a list of strings for each file, and output a JSON structured file which can be used to store the translations mapping.
Then when Crunching the script up, it uses the map to convert strings from one language into another.
eg.
The full ScriptCrusher is here..
http://www.akbkhome.com/svn/gtkDS/test/ScriptCrusher.js
Happy Hacking..
- Its slow to load
- Can you translale it into their language
- Loading of many javascript files (With our currently quite small applications, there are still more than 30 single javascript files - one class per file, which can take overĀ 10 seconds to load.)
- The javascript contains comments and whitespace (As I like to make my code readable and maintainable)
To address these issues, there are a few javascript compression tools out there, however, most have rather painful setup requirements, like getting java working, and are not very hacker friendly.
Now that all my interface is embedded into javascript code, including all the button labels etc. l have been wondering for quite a while how I was going to translate the application and keep it up-to-date (in the Flexy template engine we extract the strings from the HTML, and have a tool that manages PO files based on them). Since this did not seem feasible for Javascript, I was begining to dread the thought of finding all the strings and wrapping them with something like a gettext type call.
So it turned out my little dmdscript hacking may have some use...
It took a small amount of hacking to add tokenizing support to dmdscript, by adding a new class for the javascript interface and few minor changes to dmdscript's internal tokenizer I got this little script to work.
var data = File.read(fname);
var x = Script.tokenize(data);
for(var i =0; i < x.length; i++) {
if (x[i].tok == "whitespace") {
if (x[i].val.indexOf("\n") > -1) {
print("\n");
continue;
}
print(" ");
continue;
}
print(x[i].val);
}
print(";\n");
Basically it reads the file, then creates an array of objects, each one representing a token. (I should perhaps change the token to an integer and useĀ Script.Token.whitespace as a constant... ).
The script will basically strip all the whitespace and comments, and convert whitespace into either a line break or a space.. (as linebreaks are actually relivant in Javascript).
The next issue was how to deal with language conversion. The code above can be used to test for strings by doing:
for(var i =0; i < x.length; i++) {
if (x[i].tok == "string") {
println("got string: " + x[i].val);
}
}
So I came up with the idea of using the quote style to determine if a string needs translating.
single quotes = does not need translating
double quotes = a translable string.
It was amazingly easy to change my existing code to do this, I could then generate a list of strings for each file, and output a JSON structured file which can be used to store the translations mapping.
#djs ScriptCrusher.js -- --strings /tmp/Hello.js > /tmp/zh_HK.jsWhich can be edited and translated.
Would generate a file:
"Hello.js" : {
"Hello": "Hello",
"World": "World",
}
Then when Crunching the script up, it uses the map to convert strings from one language into another.
eg.
#djs ScriptCrusher.js -- --langfile /tmp/zh_HK.js /tmp/Hello.js \
> /tmp/Hello.zh_HK.js
#djs ScriptCrusher.js --/tmp/Hello.js > /tmp/Hello.en.js
(I've not done the merging of old/new yet, but it should be pretty trivial).
djs should be pretty simple to build: (requires gdc - available in Debian and Ubuntu + probably others)
#svn co http://www.akbkhome.com/svn/gtkDS
#cd gtkDS
#sh djs.gdc.sh
The full ScriptCrusher is here..
http://www.akbkhome.com/svn/gtkDS/test/ScriptCrusher.js
Happy Hacking..
Mentioned By:
google.com : december (61 referals)
google.com : JAVASCRIPT crusher (50 referals)
www.planet-php.net : Planet PHP (23 referals)
www.urlfan.com : extjs.com - How popular is extjs.com? (://URLFAN) (9 referals)
google.com : Crusher (9 referals)
google.com : php code crusher (8 referals)
google.com : translale (8 referals)
google.com : www.translale.google.com (8 referals)
google.com : extjs gzip (7 referals)
google.com : google translale (7 referals)
google.com : php convert whitespace (7 referals)
google.com : PHP script whitespace (7 referals)
google.com : translale google.com (6 referals)
google.com : extjs speed (5 referals)
google.com : gzip javascript php (5 referals)
google.com : svn php script (5 referals)
google.com : inurl:view:site:http://www.codepost.org% (4 referals)
del.icio.us : matix.bg's bookmarks on del.icio.us (4 referals)
ma.gnolia.com : DragonI's Bookmarks Tagged With "extJS" (3 referals)
google.com : "javascript crusher" (3 referals)
google.com : december (61 referals)
google.com : JAVASCRIPT crusher (50 referals)
www.planet-php.net : Planet PHP (23 referals)
www.urlfan.com : extjs.com - How popular is extjs.com? (://URLFAN) (9 referals)
google.com : Crusher (9 referals)
google.com : php code crusher (8 referals)
google.com : translale (8 referals)
google.com : www.translale.google.com (8 referals)
google.com : extjs gzip (7 referals)
google.com : google translale (7 referals)
google.com : php convert whitespace (7 referals)
google.com : PHP script whitespace (7 referals)
google.com : translale google.com (6 referals)
google.com : extjs speed (5 referals)
google.com : gzip javascript php (5 referals)
google.com : svn php script (5 referals)
google.com : inurl:view:site:http://www.codepost.org% (4 referals)
del.icio.us : matix.bg's bookmarks on del.icio.us (4 referals)
ma.gnolia.com : DragonI's Bookmarks Tagged With "extJS" (3 referals)
google.com : "javascript crusher" (3 referals)
Follow us
-
- Some thoughts on the language server and its usefulness in the roobuilder
- Roo Builder for Gtk4 moving forward
- Clustered Web Applications - Mysql and File replication
- GitLive - Branching - Merging
- PDO_DataObject Released
- PDO_DataObject is under way
- Mass email Marketing and anti-spam - some of the how-to..
- Hydra - Recruitment done right
Blog Latest
-
Twitter - @Roojs